package com.longcai.cm.moudules.system.controller;

import com.longcai.cm.base.aop.Log;
import com.longcai.cm.base.bean.PageVo;
import com.longcai.cm.base.bean.Pageable;
import com.longcai.cm.bean.R;
import com.longcai.cm.common.utils.RequestUtil;
import com.longcai.cm.moudules.system.domain.SysDicValue;
import com.longcai.cm.moudules.system.service.SysDicValueService;
import com.longcai.cm.moudules.system.utils.ShiroUtils;
import com.longcai.cm.utils.DateUtils;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;

/**
 * 【系统模块】====>【数据字典值】
 *
 * @author code
 * @Description: 数据字典
 * @file:
 * @throws
 * @date 2018-12-25
 */
@RestController
@RequestMapping("/sysDicValue")
@Api(value = "系统管理-数据字典值", tags = "系统管理-数据字典值")
@ApiSort(6)
public class SysDicValueController {


    @Resource
    private SysDicValueService sysDicValueService;


    /**
     * 【API】====>【系统模块】====>【数据字典值】===>获取列表数据
     *
     * @param request  [请求信息]
     * @param response [响应信息]
     * @return R
     */
    @PostMapping("/getList")
    @ApiOperation(value = "【S6001】获取数据字典值列表", notes = "获取数据字典值列表")
    @ApiOperationSupport(order = 1)
    @Log(value = "获取数据字典值列表", type = "1")
    public R<PageVo<SysDicValue>> getList(HttpServletRequest request, HttpServletResponse response) {
        Pageable pageInfo = RequestUtil.getPageable(request);
        PageVo<SysDicValue> result = sysDicValueService.getPageBean(pageInfo, SysDicValue.class);
        return R.success(result);
    }


    /**
     * 【API】====>【系统模块】====>【数据字典值】===>保存数据
     *
     * @param vo
     * @param request  [请求信息]
     * @param response [响应信息]
     * @return R
     */
    @PostMapping("/save")
    @ApiOperation(value = "【S6002】保存数据字典值", notes = "保存数据字典值")
    @ApiOperationSupport(order = 2)
    @Log(value = "保存数据字典值", type = "1")
    public R save(SysDicValue vo, HttpServletRequest request, HttpServletResponse response) {
        String id = request.getParameter("id");

        if (id != null && id != "") {
            sysDicValueService.updateById(vo);
        } else {
            vo.setId(UUID.randomUUID().toString().replace("-", ""));
            vo.setCreateTime(DateUtils.getTimeNow());
            vo.setCreateUser(ShiroUtils.getUserId());
            sysDicValueService.save(vo);
        }

        return R.success();
    }


    /**
     * 删除数据字典值
     *
     * @param request  [请求信息]
     * @param response [响应信息]
     * @return R
     */
    @PostMapping("/delete")
    @ResponseBody
    @ApiOperation(value = "【S5004】删除数据字典值", notes = "删除数据字典值")
    @ApiOperationSupport(order = 4)
    @Log(value = "删除数据字典值", type = "1")
    public R delete(HttpServletRequest request, HttpServletResponse response) {
        response.setContentType("text/html;charset=utf-8");
        String ids = request.getParameter("ids");
        String[] idStrings = ids.split(",");
        for (String idString : idStrings) {
            sysDicValueService.removeById(idString);
        }
        return R.success();
    }

}
